package edu.washington.ling.syntric.struct.error;

import java.util.ArrayList;

import edu.washington.ling.syntric.util.Utils;

public class TreeMergeException extends Exception {

	private static final long serialVersionUID = 2457979353175358883L;
	private ArrayList<String> subErrors;
	
	public TreeMergeException(String message) {
		super(message);
		subErrors = null;
	}
	public TreeMergeException(String message, ArrayList<String> subErrors) {
		super(message);
		this.subErrors = subErrors;
	}
	public TreeMergeException(String message, String layer1, String yield1, 
			String layer2, String yield2) {
		super(message);
		this.subErrors = new ArrayList<String>();
		subErrors.add(String.format("%s: %s", layer1, yield1));
		subErrors.add(String.format("%s: %s", layer2, yield2));
	}
	public boolean hasSubErrors() {
		return (subErrors != null);
	}
	public String getSubErrorStr() {
		ArrayList<String> tabSubErrors = new ArrayList<String>();
		for (String subError: subErrors) {
			subError = "\t" + subError;
			subError = subError.replaceAll("\n", "\n\t");
			tabSubErrors.add(subError);
		}
		return Utils.join(tabSubErrors, "\n");
	}
	@Override
	public String getMessage() {
		String message = super.getMessage();
		if (this.hasSubErrors()) {
			return String.format("%s\n%s", message, getSubErrorStr());
		}
		else { return message; }
	}
	
}
